<?php

class Admin_Model_Image extends Zend_Db_Table_Abstract {

    protected $_name = 'images';
    protected $_primary = 'id';
    protected $_referenceMap = array(
    		'product_feature_detail'=>array(
    				'columns' => array('productFeatureDetailId'),
    				'refTableClass' => 'Admin_Model_ProductFeatureDetail',
    				'refColumns' => array('id'),
    				'onDelete' => self::CASCADE,
    				'onUpdate' => self::CASCADE,
    		)
    
    );

    public function getImages() {
        $select = $this->select();
        $result = $this->fetchAll($select);
        if ($result) {
            return $result->toArray();
        } else {
            return null;
        }
    }

    public function getImageOne($id) {
        $select = $this->select()
                ->where('id = ?', (int) $id);

        $result = $this->fetchRow($select);
        if ($result) {
            return $result->toArray();
        } else {
            return null;
        }
    }

    public function getImageByProductId($productId) {
        $select = $this->select()
                ->where('productId = ?', (int) $productId);

        $result = $this->fetchAll($select);
        if ($result) {
            return $result->toArray();
        } else {
            return null;
        }
    }

    public function addItem($info) {
    	try {
	        $id = $this->insert($info);
	
	        return $id;
        } catch (Exception $e) {
        	throw $e;
        }
    }

    public function updateItem($id, $info) {
    	try {
	        $result = $this->update($info, "id=" . (int) $id);
	        return $result;
        } catch (Exception $e) {
        	throw $e;
        }
    }

    public function deleteItem($id) {
    	try {
	        $result = $this->delete("id = " . (int) $id);
	        
	       	return $result;
        } catch (Exception $e) {
        	throw $e;
        }
    }

    public function countItems() {
        $query = $this->select()
                ->from($this->_name, 'count(id) as total');
        $result = $this->fetchRow($query);
        if ($result) {
            return $result->total;
        } else {
            return null;
        }       
    }

}